//
//  CmtAttentionTableViewController.m
//  Products_B
//
//  Created by lanou on 16/5/28.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CmtAttentionTableViewController.h"
#import "CmtSecMoreLikeViewController.h"

#import "RequestManager.h"//网络请求类
#import "CmtAttenCellTableViewCell.h"//社区中关注Cell
#import "CmtRecommendModel.h"//大Model
#import "CmtRecommendComment.h"//评论
#import "CmtRecommendLike.h"//喜欢
#import "CmtRecommendTopics.h"//top
#define kCmtUrl @"http://api.izhangchu.com/?=user_id=1514218&page=1&token=289466943102E6204C7EE83EF061FCE4&methodName=ShequFollow&size=20&version=4.40&"


#import "CmtProductDetailViewController.h"//作品详情
#import "CmtShenQuViewController.h"//厨师达人相关界面

@interface CmtAttentionTableViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)NSMutableArray *modelArray;

@property(nonatomic,strong)NSMutableArray *likeArray;

@property(nonatomic,assign)int page;

@property(nonatomic,assign)CGPoint OldContentOffset;
@property(nonatomic,assign)CGPoint NewContentOffset;
@property(nonatomic,assign)CGPoint ContentOffset;
@end

@implementation CmtAttentionTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self.tableView registerNib:[UINib nibWithNibName:@"CmtAttenCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"cmtCell"];
    self.modelArray = [[NSMutableArray alloc] initWithCapacity:1];
    self.likeArray = [[NSMutableArray alloc] initWithCapacity:1];
    [self requestDataWithUrlString:kCmtUrl];
    
    self.page = 1;
//    上拉加载更多
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page += 1;
        [self requestDataWithUrlString:kCmtUrl];
    }];
//   下拉刷新
    self.tableView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self.modelArray removeAllObjects];
        [self.likeArray removeAllObjects];
        [self requestDataWithUrlString:kCmtUrl];
        
    }];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    NSLog(@"开始拖拽%lf",scrollView.contentOffset.y);
    self.ContentOffset = scrollView.contentOffset;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //    NSLog(@"滚动%lf",scrollView.contentOffset.y);
    self.NewContentOffset = scrollView.contentOffset;
    
    //    向下滑动
    if (self.NewContentOffset.y > self.ContentOffset.y | self.NewContentOffset.y > self.OldContentOffset.y) {
        NSLog(@"向下滑");
        
        self.CmtAttenBlock(YES);
        
    }else{
        
        NSLog(@"向上滑");
        self.CmtAttenBlock(NO);
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"开始减速%lf",scrollView.contentOffset.y);
    self.OldContentOffset = scrollView.contentOffset;
}

//数据请求
-(void)requestDataWithUrlString:(NSString *)urlString{

    [RequestManager requestWithUrl:urlString requestType:RequestTypeGET parDic:nil finish:^(NSData *data) {
        
        NSDictionary *jsDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSArray *dataArr = jsDic[@"data"][@"data"];
        for (NSDictionary *dic in dataArr) {
            CmtRecommendModel *recModel = [[CmtRecommendModel alloc]init];
            
            NSArray *likeArr = dic[@"like"];
            
            for (NSDictionary *likeDic in likeArr) {
                CmtRecommendLike *likeModel = [[CmtRecommendLike alloc]init];
                [likeModel setValuesForKeysWithDictionary:likeDic];
                
                [recModel.like addObject:likeModel];
                
            }
            
            [recModel setValuesForKeysWithDictionary:dic];

            [_modelArray addObject:recModel];
        }
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    } error:nil];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CmtAttenCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cmtCell" forIndexPath:indexPath];
    CmtRecommendModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    
    
//    给点赞的头像添加手势
    cell.head_img.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(CmtAttenCellTap:)];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(CmtAttenCellTap:)];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(CmtAttenCellTap:)];
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(CmtAttenCellTap:)];
    UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(CmtAttenCellTap:)];
    UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(CmtAttenCellTap:)];

    [cell.head_img addGestureRecognizer:tap];
    cell.head_img.tag = 10 + indexPath.row;
    
    [cell.image1 addGestureRecognizer:tap1];
    cell.image1.tag = 2000 + indexPath.row;
    [cell.image2 addGestureRecognizer:tap2];
    cell.image2.tag = 3000 + indexPath.row;
    [cell.image3 addGestureRecognizer:tap3];
    cell.image3.tag = 4000 + indexPath.row;
    [cell.image4 addGestureRecognizer:tap4];
    cell.image4.tag = 5000 + indexPath.row;
    [cell.image5 addGestureRecognizer:tap5];
    cell.image5.tag = 6000 + indexPath.row;
    
    cell.cellblock = ^(NSString *_id){
        
        self.post_idBlock(_id);
    };
    
    return cell;
}


//手势
-(void)CmtAttenCellTap:(UITapGestureRecognizer *)tap{
    
    NSLog(@"点击了手势");
    NSInteger index = 0;
    NSInteger count = tap.view.tag;
    if ( count > 0 && count < 2000) {
        index = count - 10;
     CmtRecommendModel *model = self.modelArray[index];
        
        self.user_idBlock(model.user_id);
        
    }else{
        CmtRecommendModel *model = self.modelArray[index];
        self.likeArray = model.like[index];
        if (count > 2000 && count< 3000) {
            index = 0;
        }
        if (count > 3000 && count < 4000) {
            index = 1;
        }
        if (count > 4000 && count < 5000) {
            index = 2;
        }
        if (count > 5000 && count < 6000) {
            index = 3;
        }if (count > 6000) {
            index = 4;
        }
        NSDictionary *likeModel = (NSDictionary *)model.like[index];
        
        
        self.user_idBlock(likeModel[@"user_id"]);
    }
    
}

//返回Cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 430;
}

//点击Cell的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

   
    CmtRecommendModel *model = self.modelArray[indexPath.row];
    
    self.block(model._id);

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
